#pragma once

#include "LvDevice.hpp"
#include "LvSwapChain.hpp"
#include "LvWindow.h"


#include <cassert>
#include <memory>
#include <vector>


class LvRenderer
{
public:
	LvRenderer(LvWindow& window, LvDevice& device);
	~LvRenderer();

	LvRenderer(const LvRenderer&) = delete;
	LvRenderer& operator=(const LvRenderer&) = delete;

	VkRenderPass getSwapChainRendPass()
	{
		return lvSwapChain->getRenderPass();
	}

	bool isFrameInProgress() const
	{
		return isFrameStarted;
	}

	VkCommandBuffer getCurrentCommandBuffer() const
	{
		assert(isFrameStarted && "Cannot get command buffer when frame not in progress");
		return commandBuffers[currentFrameIndex];
	}

	int getFrameIndex() const
	{
		assert(isFrameStarted && "Cannot get frame index when frame not in progress");
		return currentFrameIndex;
	}

	VkCommandBuffer beginFrame();
	void endFrame();
	void beginSwapChainRenderPass(VkCommandBuffer commandBuffer);
	void endSwapChainRenderPass(VkCommandBuffer commandBuffer);

private:
	void createCommandBuffers();
	void freeCommandBuffers();
	void recreateSwapChain();

	LvWindow& lvWindow;
	LvDevice& lvDevice;
	std::unique_ptr<LvSwapChain> lvSwapChain;
	std::vector<VkCommandBuffer> commandBuffers;

	uint32_t currentImageIndex{ 0 };
	int currentFrameIndex{ 0 };
	bool isFrameStarted{ false };
};

